package cn.drenal.capcap.service.impl;

import cn.drenal.capcap.entity.Artwork;
import cn.drenal.capcap.entity.ArtworkTag;
import cn.drenal.capcap.entity.RecommendedArtwork;
import cn.drenal.capcap.entity.Tag;
import cn.drenal.capcap.entity.vo.RecommendedArtworkVO;
import cn.drenal.capcap.mapper.ArtworkTagMapper;
import cn.drenal.capcap.mapper.RecommendedArtworkMapper;
import cn.drenal.capcap.mapper.TagMapper;
import cn.drenal.capcap.mapper.UserMapper;
import cn.drenal.capcap.service.ArtworkService;
import cn.drenal.capcap.service.RecommendedArtworkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 推荐作品服务实现类
 */
@Service
@RequiredArgsConstructor
public class RecommendedArtworkServiceImpl extends ServiceImpl<RecommendedArtworkMapper, RecommendedArtwork> implements RecommendedArtworkService {

    private final ArtworkService artworkService;
    private final UserMapper userMapper;
    private final ArtworkTagMapper artworkTagMapper;
    private final TagMapper tagMapper;

    @Override
    public Page<RecommendedArtwork> page(Integer current, Integer size, String reason, Integer status) {
        Page<RecommendedArtwork> page = new Page<>(current, size);
        LambdaQueryWrapper<RecommendedArtwork> wrapper = new LambdaQueryWrapper<>();
        
        // 根据推荐理由模糊查询
        wrapper.like(StringUtils.isNotBlank(reason), RecommendedArtwork::getReason, reason);
        // 根据状态查询
        wrapper.eq(status != null, RecommendedArtwork::getStatus, status);
        // 按排序和创建时间排序
        wrapper.orderByAsc(RecommendedArtwork::getSort);
        wrapper.orderByDesc(RecommendedArtwork::getCreateTime);
        
        return page(page, wrapper);
    }

    @Override
    public List<RecommendedArtworkVO> getLatestRecommendedArtworks(Integer limit) {
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        
        // 构建查询条件
        LambdaQueryWrapper<RecommendedArtwork> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RecommendedArtwork::getStatus, 1); // 只查询启用状态的
//        wrapper.le(RecommendedArtwork::getStartTime, now); // 开始时间小于等于当前时间
//        wrapper.gt(RecommendedArtwork::getEndTime, now).or().isNull(RecommendedArtwork::getEndTime); // 结束时间大于当前时间或为空
//
        // 按创建时间倒序排序，获取最新的
        wrapper.orderByDesc(RecommendedArtwork::getCreateTime);
        wrapper.last("limit " + limit);
        
        // 限制返回数量
        Page<RecommendedArtwork> page = page(new Page<>(1, limit), wrapper);
        
        // 转换为VO
        return convertToVOList(page.getRecords());
    }
    
    /**
     * 将推荐作品实体转换为VO列表
     *
     * @param recommendedArtworks 推荐作品列表
     * @return 推荐作品VO列表
     */
    private List<RecommendedArtworkVO> convertToVOList(List<RecommendedArtwork> recommendedArtworks) {
        List<RecommendedArtworkVO> voList = new ArrayList<>();
        
        for (RecommendedArtwork recommendedArtwork : recommendedArtworks) {
            // 获取作品信息
            Artwork artwork = artworkService.getById(recommendedArtwork.getArtworkId());
            if (artwork != null) {
                RecommendedArtworkVO vo = new RecommendedArtworkVO();
                BeanUtils.copyProperties(artwork, vo);
                vo.setArtworkId(artwork.getId());
                vo.setAuthorName(userMapper.selectById(artwork.getUserId()).getUsername());
                vo.setTags(artworkTagMapper.selectList(new LambdaQueryWrapper<ArtworkTag>()
                        .eq(ArtworkTag::getArtworkId, artwork.getId())).stream()
                        .map(artworkTag -> tagMapper.selectById(artworkTag.getTagId()).getName()
                ).toList());
                voList.add(vo);
            }
        }
        
        return voList;
    }
}